package models

import (
	"fmt"
	_ "fmt"
	"gitchina/kly_service/data"
	"gitchina/kly_service/errors"
	"gitchina/kly_service/util"
)

type WalletPay struct{}

func (wp *WalletPay) PreTopUp(memberId int, amount float32) (payId ID, err error) {
	return
}

func (wp *WalletPay) PostTopUp(p data.PaymentDto, i PostPayItem) (err error) {
	return
}

func (wp *WalletPay) PrePayDeposit(item PayItem) (ID, error) {
	payId, err := wp.prePay(item)
	PostPay(payId, item.TotalAmount, PostPayItem{PayOk: true})
	return payId, err
}

func (wp *WalletPay) DoPayDeposit(payId ID) (interface{}, error) {
	fmt.Println("---------------------------")
	return new(PayRS), nil
}

func (wp *WalletPay) PostPayDeposit(p data.PaymentDto, i PostPayItem) (err error) {
	return wp.postPay(p, i)
}

func (wp *WalletPay) PrePayWaybill(item PayItem) (ID, error) {
	payId, err := wp.prePay(item)
	PostPay(payId, item.TotalAmount, PostPayItem{PayOk: true})
	return payId, err
}

func (wp *WalletPay) DoPayWaybill(payId ID) (interface{}, error) {
	return new(PayRS), nil
}

func (wp *WalletPay) PostPayWaybill(p data.PaymentDto, i PostPayItem) (err error) {
	return wp.postPay(p, i)
}

func (wp *WalletPay) prePay(item PayItem) (paymentId ID, err error) {
	var w Wallet
	var walletDto data.WalletDto
	var flowId, payId int64
	repo := &data.PayRepo{}
	w.repo = repo
	if walletDto, err = w.repo.GetWalletWithPwd(item.MemberId, item.WalletPwd); err == nil {
		if walletDto.Balance-item.TotalAmount >= 0 {
			repo.BeginTx()
			defer func() {
				repo.EndTx(err == nil)
			}()
			remark := "付订金"
			if item.PayFor == 1 {
				remark = "付运费"
			}
			if flowId, err = repo.NewTransFlow(data.TransFlowDto{
				MemberId: item.MemberId,
				TransNo:  item.TransNo,
				Amount:   item.TotalAmount,
				TypeId:   item.TypeId,
				State:    0,
				Remark:   remark,
			}); err != nil {
				util.GetLogger().Error("[PayWallet-prePay] - error:%s", err.Error())
			} else {
				if payId, err = repo.NewPayment(data.PaymentDto{
					FlowId:          flowId,
					MemberId:        item.MemberId,
					WaybillId:       item.WaybillId,
					BillAmount:      item.BillAmount,
					InvoiceAmount:   item.InvoiceAmount,
					InsuranceAmount: item.InsuranceAmount,
					DepositAmount:   item.DepositAmount,
					TotalAmount:     item.TotalAmount,
					TypeId:          item.TypeId,
					PayFor:          item.PayFor,
					CardId:          0,
					State:           0,
				}); err != nil {
					util.GetLogger().Error("[PayWallet-prePay] - error:%s", err.Error())
				} else {
					if err = w.updateBalance(walletDto, 3, flowId, -item.TotalAmount, 0, 0); err == nil {
						paymentId.Encode(ID_Shard, ID_Ver, ID_Payment_Category, payId)
					}
				}
			}
		} else {
			err = errors.New(errors.MSG_Wallet_Not_Enough_Balance_To_Pay)
		}
	} else {
		util.GetLogger().Error("[PayWallet-prePay] - error:%s", err.Error())
		err = errors.New(errors.MSG_Wallet_Invalid_Pwd)
	}
	return
}

func (wp *WalletPay) postPay(p data.PaymentDto, i PostPayItem) (err error) {
	repo := &data.PayRepo{}
	repo.BeginTx()
	defer func() {
		if err != nil {
			util.GetLogger().Error("[model-PostPayDeposit] - error%s", err.Error())
		}
		repo.EndTx(err == nil)
	}()

	if err = repo.UpdatePaymentState(p.Id, i.PayOk, ""); err == nil {
		err = repo.UpdateTransFlowState(p.FlowId, i.PayOk, i.Message)
	}

	return
}

func (wp *WalletPay) Confirm(memberID, payID ID, params string) error {
	return nil
}
